<?php
namespace Models\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class ReviewsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function postReview($data)
    {
		$data = array(
			'review' 		=> $data['review'], 			
			'user_id'  	 	=> $data['user_id'], 	
			'tracking_id' 	=> $data['trackingID'],   
			'date_added'	=> date('Y-m-d H:i:s'),
			'date_updated'	=> date('Y-m-d H:i:s'),			
			'status'	 	=> 0, 				
		);	
		$insertresult=$this->tableGateway->insert($data);	
		return $insertresult;	
    }
	public function getReviewsAndRatings($user_id){        
		$select = $this->tableGateway->getSql()->select();
		$select->join('review_replies', new Expression('review_replies.review_id=reviews.review_id'),array('reply_text'),'left');
		$select->join('rating', new Expression('rating.tracking_id=reviews.tracking_id'),array('rating'),'left');
		$select->join('order_delivery_details', new Expression('order_delivery_details.user_medicine_order_id=reviews.tracking_id'),array('shop_id'),'left');
		$select->join('user', new Expression('user.user_id=order_delivery_details.shop_id'),array('shop_id'=>'user_id','shop_name'=>'user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('city','state','country','address'),'left');		
		$select->where('reviews.user_id="'.$user_id.'"');
		$select->Group('reviews.review_id');		
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet;
    }	
	public function getAllReviewsAndRatings(){        
		$select = $this->tableGateway->getSql()->select();
		$select->join('review_replies', new Expression('review_replies.review_id=reviews.review_id'),array('reply_text'),'left');
		$select->join('rating', new Expression('rating.tracking_id=reviews.tracking_id'),array('rating'),'left');
		$select->join('order_delivery_details', new Expression('order_delivery_details.user_medicine_order_id=reviews.tracking_id'),array('shop_id'),'left');
		$select->join('user', new Expression('user.user_id=reviews.user_id'),array('user_id','user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('city','state','country','address'),'left');
		$select->join(array('shop'=>'user'), new Expression('shop.user_id=order_delivery_details.shop_id'),array('shop_id'=>'user_id','shop_name'=>'user_name'),'left');
		$select->join(array('shop_details'=>'user_details'), new Expression('shop_details.user_id=shop.user_id'),array('shop_city'=>'city','shop_state'=>'state','shop_country'=>'country','shop_address'=>'address'),'left');
		$select->where('order_delivery_details.admin_process_status="1"');
		$select->Group('reviews.review_id');		
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet;
    }	
}